//
//  WYSendAdvertiseController.m
//  CirclePush
//
//  Created by admin on 2018/4/10.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYSendAdvertiseController.h"
#import "WYRecommendCell.h"
#import "WYDirectMailHeadView.h"
#import "WYMediaLibraryController.h"
@interface WYSendAdvertiseController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong) WYImageModel *imageMedel;
@property (nonatomic,strong) WYDirectMailHeadView *directMailHeadView;


@property (nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation WYSendAdvertiseController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray=[[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadImage];
    [self loadData];
}
-(void)loadData
{
    
    WEAKSELF
    [WYHttpServer getMediaSuccessWithPageNum:@"1" WithPageSize:@"3" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            if ([WYMedia getMediaListFromJson:response[@"result"]].count) {
                [weakSelf.dataArray addObject:[WYMedia getMediaListFromJson:response[@"result"]]];
                [weakSelf.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            }
            
        }else{
            
        }
        
        
        
        
    }];
    
    
    
    
    
}

-(void)loadImage
{
    WEAKSELF
    [WYHttpServer getImageListWithtType:@"3" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            NSArray *array = [WYImageModel getImageModelFromJson:response[@"result"]];
            if (array.count) {
                weakSelf.imageMedel = array[0];
               // [weakSelf.tableView reloadData];
//                [weakSelf.directMailHeadView.headImageView sd_setImageWithURL:[NSURL URLWithString:weakSelf.imageMedel.image]];
                
                
                [weakSelf.directMailHeadView.headImageView sd_setImageWithURL:[NSURL URLWithString:weakSelf.imageMedel.image] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                    if (image.size.height) {
                        /**  < 图片宽度 >  */
                        CGFloat imageW = [UIScreen mainScreen].bounds.size.width - 2 * 8;
                        /**  <根据比例 计算图片高度 >  */
                        CGFloat ratio = image.size.height / image.size.width;
                        /**  < 图片高度 + 间距 >  */
                        CGFloat imageH = ratio * imageW + 15;
                        /**  < 缓存图片高度 没有缓存则缓存 刷新indexPath >  */
                        [weakSelf.directMailHeadView.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                            make.top.mas_equalTo(0);
                            make.left.mas_equalTo(0);
                            make.size.mas_equalTo(CGSizeMake(ScreenWidth, imageH));
                            
                            [self.tableView beginUpdates];
                            CGRect newFrame = self.directMailHeadView.frame;
                            newFrame.size.height = newFrame.size.height-200*DUP+imageH;
                            self.directMailHeadView.frame = newFrame;
                            [self.tableView setTableHeaderView:self.directMailHeadView];
                            [self.tableView endUpdates];
                            
                            
                            
                            
                        }];
                        
                        
                        
                    }
                }];
                
                
                
                
                
                
            }
        }
    }];
    
    
    
    
    [WYHttpServer getImageListWithtType:@"4" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            NSArray *array = [WYImageModel getImageModelFromJson:response[@"result"]];
            if (array.count) {
                WYImageModel *imageMedel = array[0];
                // [weakSelf.tableView reloadData];
//                [weakSelf.directMailHeadView.bottomImageView sd_setImageWithURL:[NSURL URLWithString:imageMedel.image]];

                [weakSelf.directMailHeadView.bottomImageView sd_setImageWithURL:[NSURL URLWithString:imageMedel.image] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    
                    if (image.size.height) {
                        //                        /**  < 图片宽度 >  */
                    CGFloat imageW = [UIScreen mainScreen].bounds.size.width - 2 * 8;
                        //                        /**  <根据比例 计算图片高度 >  */
                    CGFloat ratio = image.size.height / image.size.width;
                        //                        /**  < 图片高度 + 间距 >  */
                    CGFloat imageH = ratio * imageW + 15;
                        
                        [weakSelf.directMailHeadView.bottomImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(self.directMailHeadView.serviceProcessImageView.mas_bottom).with.offset(10);
                            make.left.mas_equalTo(0);
                            make.size.mas_equalTo(CGSizeMake(ScreenWidth, imageH));
                        }];
                        [self.tableView beginUpdates];
                        CGRect newFrame = self.directMailHeadView.frame;
                        newFrame.size.height = newFrame.size.height-125*DUP+imageH;
                        self.directMailHeadView.frame = newFrame;
                        [self.tableView setTableHeaderView:self.directMailHeadView];
                        [self.tableView endUpdates];
                        
                    }
                    
                    
                }];
            }
        }
    }];
    
    
    
    
    
    
    
}



-(void)setupUI
{
    
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYRecommendCell" bundle:nil] forCellReuseIdentifier:@"WYRecommendCell"];
    WYDirectMailHeadView *hvc = [[WYDirectMailHeadView alloc]init];
    self.directMailHeadView = hvc;
    WEAKSELF
    hvc.complete = ^(NSString *userName, NSString *userMobile) {
        [weakSelf updateMessageWithUserMessage:userName WithTelephone:userMobile];
    };
    
    
    hvc.frame = CGRectMake(0, 0, ScreenWidth, SYRealValue(1650 / 2));
    self.tableView.tableHeaderView = hvc;
    self.tableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);
    
//    UIImageView *imageView = [[UIImageView alloc]init];
//    [self.view addSubview:imageView];
//    imageView.frame = CGRectMake(0, 0, 200, 200);
//    self.imageView = imageView;
//    imageView.backgroundColor = [UIColor redColor];
    
}

-(void)updateMessageWithUserMessage:(NSString *)userMessage WithTelephone:(NSString *)phone
{
   
    if (!userMessage.length) {
        [self toast:@"请输入姓名"];
    }
    
    if (!phone.length) {
        [self toast:@"请输入手机号"];
    }
    
    WEAKSELF
    [WYHttpServer submissiondemandWithLinkeMan:userMessage WithTelephone:phone WithEmail:nil WithMediaType:@"2" WithMediaId:nil SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            self.directMailHeadView.linkPhoneManTextField.text=@"";
            self.directMailHeadView.linkManTextField.text=@"";
            [weakSelf toast:@"提交成功"];
        }else{
            [weakSelf toast:errorMessage];
        }
        
        
        
    }];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[section]count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYRecommendCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"WYRecommendCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYRecommendCell" owner:nil options:nil].firstObject;
    }
    WYMedia *media = self.dataArray[indexPath.section][indexPath.row];
    [cell setMedia:media];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYMediaLibraryController *mlbc = [[WYMediaLibraryController alloc]init];
    mlbc.updateUserImage =  ^(UIImage *image){
        self.imageView.image = image;
    };
  //  WEAKSELF
    [self presentViewController:mlbc animated:YES completion:nil];
    
    
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource=self;
        
        CGFloat height = 64;
        if (kDevice_Is_iPhoneX) {
            height = 88;
        }
        
        _tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-height);
    }
    
    
    return _tableView;
}







@end
